var ObjectId = require('mongodb').ObjectId;
import app from 'src/app';
import * as request from 'request'
import mongodbHandle from './mongodbHandle';

class GameHandle {
    reserveData: any = {}
    constructor() {
        this.init()
    }
    init = async () => {
        setTimeout(() => {
            this.requestData()
        }, 3000)
        setInterval(() => {
            this.requestData()
        }, 5 * 60 * 1000)

    }
    requestData = async () => {
        let reserveDataUrl = app.config.reserveDataUrl

        if (!reserveDataUrl) {
            return 0
        }

        await request.get(reserveDataUrl, async (err, res, body) => {
            if (err) {
                console.log('err:', err)
                return
            }
            let data = JSON.parse(body)
            let num = 0
            if (typeof data.data.reservation == 'string') {
                num = Number(data.data.reservation.replace(/,/g, ''))
            }
            let nowTime = new Date().getTime()
            this.reserveData.time = nowTime
            this.reserveData.num = num
        })
    }
    getReserveData = () => {
        if (this.reserveData) {
            return this.reserveData.num || 0
        } else {
            return 0
        }
    }
    //累计充值
    sendCumulativeRecharge = async (pid, money) => {
        let cumulativeRecharge = app.config.cumulativeRecharge

        if (money >= cumulativeRecharge[0].money) {
            let doc = await mongodbHandle.playerDao.findOne({ _id: ObjectId(pid) })

            if(doc){
                doc.cumulativeRecharge = doc.cumulativeRecharge || {}
                let updateFlag = false
                for (let i = 0; i < cumulativeRecharge.length; i++) {
                    let item = cumulativeRecharge[i]
                    if (money >= item.money) {
                        let itemList = item.itemList
                        let items = []
                        let items2 = []
                        let initNum = doc.cumulativeRecharge[item.money] || 0
                        if (initNum < itemList.length) {
                            for (let j = initNum; j < itemList.length; j++) {
                                if (Array.isArray(itemList[j])) {
                                    items.push(itemList[j])
                                } else {
                                    items2.push(itemList[j])
                                }
                            }
                            if (initNum == 0) {
                                await mongodbHandle.mailDao.newAndSave(pid, `累计充值${item.money}元礼包`, items, items2)
                            } else {
                                await mongodbHandle.mailDao.newAndSave(pid, `累计充值${item.money}元礼包补偿`, items, items2)
                            }
                            doc.cumulativeRecharge[item.money] = itemList.length
                            updateFlag = true
                        }
                    } else {
                        break
                    }
                }
                if (updateFlag) {
                    await mongodbHandle.playerDao.updateOne({ _id: ObjectId(pid) }, { $set: { cumulativeRecharge: doc.cumulativeRecharge } })
                }
            }
            
        }
    }
}


let gameHandle = new GameHandle()
export default gameHandle